package com.duoduo.algs4.util;

import edu.princeton.cs.introcs.StdOut;

public class Accumulator {
	protected double total;
	protected int N;
	protected String id;

	public Accumulator(){
		
	}
	public Accumulator(String id) {
		this.id = id;
	}

	public void addDataValue(double val) {
		N++;
		total += val;
	}

	public double mean() {
		return total / N;
	}

	public String toString() {
		return String.format("%s:%d次累加的平均值为：%7.5f", id,N, mean());
	}
	public static void main(String[] args) {
		Accumulator head = new Accumulator("测试累加器");
		for (int i = 0; i < 10; i++) {
			head.addDataValue(i);
		}
		StdOut.println(head);
		assert head.mean() > 4 && head.mean() < 5 ;
	
	}
}